package com.yiran.service.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiran.mapper.AttendanceMapper;
import com.yiran.pojo.Attendance;
import com.yiran.service.AttendanceService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance>
 implements AttendanceService {
    @Override
    public List attendancList(Integer userId) {
        LocalDate now = LocalDate.now();

        // 获取月初的日期
        LocalDate startOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());

        // 获取月末的日期
        LocalDate endOfMonth = now.with(TemporalAdjusters.lastDayOfMonth());

        // 构造月初的日期时间（午夜12点）
        LocalDateTime startOfMonthDateTime = LocalDateTime.of(startOfMonth, LocalTime.MIDNIGHT);

        // 构造月末的日期时间（一天中的最后一秒）
        LocalDateTime endOfMonthDateTime = LocalDateTime.of(endOfMonth, LocalTime.MAX);


        //调用mybatisplus支持实现对当月月初到当月月末的打卡纪律
        List<Attendance> attendanceList = lambdaQuery().eq(Attendance::getUserId, userId)
                .between(Attendance::getClockingTime,startOfMonthDateTime,endOfMonthDateTime)
                .list();
        return attendanceList;
    }


    @Override
    public void add(Long id) {
        LocalDateTime now = LocalDateTime.now();
        Attendance attendance = new Attendance();
        attendance.setClockingTime(now);
        attendance.setUserId(id);
        save(attendance);
    }
}

